﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApC3DDataManagerApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPBASELib;
using EMPCLIENTCOMMANDSLib;
using EMPTYPELIBRARYLib;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands;

internal class CApC3DDataManagerApp
{
  private List<CApC3DProcessNodeData> m_rootOperations = new List<CApC3DProcessNodeData>();
  private TxTypeFilter m_operationFilter;

  internal CApC3DDataManagerApp()
  {
    Type[] typeArray;
    if (TxTcUtilities.IsTcAnyPlatform)
      typeArray = new Type[2]
      {
        typeof (TxTcProcess),
        typeof (ITxTcOperation)
      };
    else
      typeArray = new Type[1]{ typeof (ITxOperation) };
    this.m_operationFilter = new TxTypeFilter(typeArray);
  }

  internal void Initialize()
  {
    TxObjectList operationsList = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) this.m_operationFilter);
    if (((Collection<ITxObject>) operationsList).Count == 0)
      operationsList = ((ITxObjectCollection) TxApplication.ActiveDocument.OperationRoot).GetDirectDescendants((ITxTypeFilter) this.m_operationFilter);
    this.CollectAllOperations(operationsList, (CApC3DProcessNodeData) null);
  }

  private void CollectAllOperations(TxObjectList operationsList, CApC3DProcessNodeData parent)
  {
    CApC3DProcessNodeData parent1 = (CApC3DProcessNodeData) null;
    TxObjectList operationsList1 = new TxObjectList();
    foreach (ITxOperation operations in (Collection<ITxObject>) operationsList)
    {
      if (this.IsOperationTypeSupported(operations))
      {
        if (parent == null)
        {
          parent1 = (CApC3DProcessNodeData) new CApC3DRootNodeData(operations);
          this.m_rootOperations.Add(parent1);
        }
        else
          parent1 = new CApC3DProcessNodeData(operations, parent);
      }
      if (operations is ITxObjectCollection)
        operationsList1 = ((ITxObjectCollection) operations).GetDirectDescendants((ITxTypeFilter) this.m_operationFilter);
      if (((Collection<ITxObject>) operationsList1).Count > 0)
        this.CollectAllOperations(operationsList1, parent1);
    }
  }

  private bool IsOperationTypeSupported(ITxOperation currentOperation)
  {
    bool flag = false;
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      switch (currentOperation)
      {
        case ITxTcOperation _:
        case TxTcProcess _:
          flag = true;
          break;
      }
    }
    else if (currentOperation != null)
      flag = true;
    return flag;
  }

  internal List<CApC3DProcessNodeData> Roots => this.m_rootOperations;

  internal List<CApC3DProcessNodeData> GetProcesses()
  {
    List<CApC3DProcessNodeData> processes = new List<CApC3DProcessNodeData>();
    this.FillProcessesList(this.m_rootOperations, false, ref processes);
    return processes;
  }

  internal List<CApC3DProcessNodeData> GetOrderedProcesses()
  {
    List<CApC3DProcessNodeData> processes = new List<CApC3DProcessNodeData>();
    this.FillProcessesList(this.m_rootOperations, true, ref processes);
    return processes;
  }

  private void FillProcessesList(
    List<CApC3DProcessNodeData> levelList,
    bool isOrdered,
    ref List<CApC3DProcessNodeData> processes)
  {
    foreach (CApC3DProcessNodeData level in levelList)
    {
      processes.Add(level);
      this.FillProcessesList(level.GetChildren(isOrdered), isOrdered, ref processes);
    }
  }

  internal bool DoRootsHave3DData()
  {
    bool flag = false;
    foreach (CApC3DRootNodeData rootOperation in this.m_rootOperations)
    {
      if (rootOperation.DoesHave3DAttachment)
      {
        flag = true;
        break;
      }
    }
    return flag;
  }

  internal CApC3DDocumentationSettings GetDocumentationSettings()
  {
    return !this.m_rootOperations.Exists(new Predicate<CApC3DProcessNodeData>(this.IsRootNodeData)) ? new CApC3DDocumentationSettings() : (this.m_rootOperations.Find(new Predicate<CApC3DProcessNodeData>(this.IsRootNodeData)) as CApC3DRootNodeData).GetDocumentationSettings();
  }

  private bool IsRootNodeData(CApC3DProcessNodeData processNodeData)
  {
    return processNodeData is CApC3DRootNodeData;
  }

  internal void FillAllSteps()
  {
    foreach (CApC3DRootNodeData rootOperation in this.m_rootOperations)
      rootOperation.FillAllSteps();
  }

  internal bool AreRootsCheckedOut()
  {
    bool flag = true;
    if (TxApplication.PlatformType == 1)
    {
      EmpAppItemList empAppItemList = (EmpAppItemList) new EmpAppItemListClass();
      foreach (CApC3DProcessNodeData rootOperation in this.m_rootOperations)
      {
        if (!(rootOperation.PlanningNodeObject.PlatformServicesProvider is ITxEmsServicesProvider servicesProvider) || servicesProvider.CheckInCheckOutState == 2)
        {
          flag = false;
          break;
        }
        if (servicesProvider != null && servicesProvider.CheckInCheckOutState == null)
        {
          EmpAppItem empAppItem = (EmpAppItem) new EmpAppItemClass();
          // ISSUE: explicit reference operation
          ref ((IEmpAppItem) empAppItem).Key = @new EmpObjectKey()
          {
            objectId = servicesProvider.InternalId
          };
          ((IEmpAppItemList) empAppItemList).Add(empAppItem);
        }
      }
      if (flag && ((IEmpAppItemList) empAppItemList).Size > 0)
      {
        EmpCheckOutCmd empCheckOutCmd = (EmpCheckOutCmd) new EmpCheckOutCmdClass();
        EmpCheckOutCmdParams checkOutCmdParams = (EmpCheckOutCmdParams) new EmpCheckOutCmdParamsClass();
        ((IEmpCheckOutCmdParams) checkOutCmdParams).Recursive = false;
        ((IEmpCheckOutCmdParams) checkOutCmdParams).ShowDialog = false;
        ((IEmpCheckOutCmdParams) checkOutCmdParams).Selection = empAppItemList;
        ((IEmpCmd) empCheckOutCmd).Execute((IEmpCmdParams) checkOutCmdParams);
      }
    }
    return flag;
  }

  internal bool DoRootsExistOnServer()
  {
    bool flag = true;
    if (TxApplication.PlatformType == 1)
    {
      foreach (CApC3DProcessNodeData rootOperation in this.m_rootOperations)
      {
        if ((rootOperation.PlanningNodeObject.PlatformServicesProvider as ITxEmsServicesProvider).InternalId == 0)
        {
          flag = false;
          break;
        }
      }
    }
    return flag;
  }
}
